package demo6;

import java.util.Stack;

class MinStack {

    private Stack<Integer> s1;
    private Stack<Integer> minStack;

    public MinStack() {
        s1 = new Stack<>();
        minStack = new Stack<>();
    }
    
    public void push(int val) {
        if(minStack.empty()) {
            s1.push(val);
            minStack.push(val);
            return;
        }

        if(val > minStack.peek()) {
            s1.push(val);
        }else{
            s1.push(val);
            minStack.push(val);
        }
    }
    
    public void pop() {
        if(s1.empty()) {
            return;
        }

        if(s1.peek().equals(minStack.peek())) {
            s1.pop();
            minStack.pop();
        }else{
            s1.pop();
        }
    }
    
    public int top() {
        if(s1.empty()) {
            return -1;
        }
        return s1.peek();
    }
    
    public int getMin() {
        if(minStack.empty()) {
            return -1;
        }
        return minStack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */